package com.ccb.jyzx.expand.ouapi.config;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;


/**
 * @author XIEFEI
 * @date 2019-11-11 14:09
 * @Description report数据源配置类
 **/
@Configuration
@MapperScan(basePackages ={"com.ccb.jyzx.expand.ouapi.dao.report"},//1,dao层所在的包
        sqlSessionTemplateRef = "reportSqlSessionTemplate")
public class ReportDataSourceConfig {

    @Bean
    public SqlSessionTemplate reportSqlSessionTemplate() throws Exception {
        return new SqlSessionTemplate(reportSqlSessionFactory());
    }
    @Bean
    public DataSourceTransactionManager reportTransactionManager(){
        return new DataSourceTransactionManager(reportDataSource());
    }
    @Bean
    public SqlSessionFactory reportSqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(reportDataSource());
        factoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver()
                        .getResources("classpath:mapper/report/*.xml")); // 2. xml 所在路径
        return factoryBean.getObject();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.report")
    public DataSourceProperties reportDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.report.configuration")
    public DataSource reportDataSource() {
        return reportDataSourceProperties()
                .initializeDataSourceBuilder()
                .type(HikariDataSource.class) // 3. 可以显示指定连接池，也可以不显示指定；即此行代码可以注释掉
                .build();
    }

}

